const app = getApp();
const api = app.api;
const wxutil = app.wxutil;
const pageSize = 16; // 每页显示条数

Page({
  data: {
    device: {},
    actionList: [],
    focus: false, // 获取焦点
    showAction: false, // 是否显示操作菜单
    isEnd: false,
    tabsFixed: false,
    tabIndex: 0,
    pageCourse: 1,
    isEndCourse: false,
    loading: false,
    messageBrief: '',
    tabsTop: 255,
  },

  onLoad(options) {
    const orderId = options.orderId;
    const focus = options.focus;
    // 评论获取焦点展开键盘
    if (focus) {
      this.setData({
        focus: true,
      });
    }
    this.getSchoolDetail(orderId);
  },

  /**
   * 获取Tabs的高度
   */
  getTabsTop() {
    const navigateHeight = 56;
    const query = wx.createSelectorQuery();
    query.select("#tabs").boundingClientRect((res) => {
      this.setData({
        tabsTop: res.top - navigateHeight,
      });
    }).exec();
  },

  onShow() {
  
  },

  onPageScroll(event) {
    if (event.scrollTop >= this.data.tabsTop) {
      this.setData({
        tabsFixed: true,
      });
    } else {
      this.setData({
        tabsFixed: false,
      });
    }
  },

  /**
   * 跳转专业详情页
   */
  onJoinTap() {
    const orderId = this.data.device.id;
    const deviceInfo = {
      area: this.data.device.device_area,
      device_info: {
        institute_name: this.data.device.institute_name,
        device_name: this.data.device.device_name,
      },
    };
    wx.setStorageSync("deviceInfo", deviceInfo);

    wx.navigateTo({
      url: "/pages/rent-device/index?orderId=" + orderId,
    });
  },

  /**
   * 获取动态详情
   */
  getSchoolDetail(schoolId) {
    const url = api.userAPI + "rent/" + schoolId;

    wxutil.request.get(url).then((res) => {
      if (res.data.code == 200) {
        const device = res.data.data;
        device.create_time = this.getDate(device.create_time)
        device.end_time = this.getDate(device.end_time)
        this.setData({
          device: device,
        });
      }
    });
  },

  getDate(timeStamp){

    var date = new Date(timeStamp);
    var year = date.getFullYear().toString(); // 提取年份的后两位
    var month = ("0" + (date.getMonth() + 1)).slice(-2); // 月份从0开始，所以要加1，并补零
    var day = ("0" + date.getDate()).slice(-2); // 补零
    var formattedDate = year + "-" + month + "-" + day;
    return formattedDate;
  },

  /**
   * 图片预览
   */
  previewImage(event) {
    const current = event.currentTarget.dataset.src;
    const urls = this.data.device.image;

    wx.previewImage({
      current: current,
      urls: urls,
    });
  },

  /**
   * 展开操作菜单
   */
  onMoreTap() {
    let actionList = [{
      name: "分享",
      color: "#666",
      openType: "share",
    }];

    this.setData({
      showAction: true,
      actionList: actionList,
    });
  },

  /**
   * 关闭操作菜单
   */
  onCancelSheetTap() {
    this.setData({
      showAction: false,
    });
  },

  onShareAppMessage() {
    return {
      title: this.data.device.device_name,
      imageUrl: this.data.device.image,
      path: "/pages/rent-detail/index?deviceId=" + this.data.device.id,
    };
  },
});
